﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LCUV;
using System.Diagnostics;
using System.Data;
using Telerik.Web.UI;
using System.Text;
namespace sWarranty.WEB
{
    public partial class ReportSaleOutM : System.Web.UI.Page
    {
        public string _currentUserEmpNo = "";
        public string _currentUserAuthority = "";
        public string _currentUserRegion = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            _currentUserEmpNo = GetUserLoginEmpNo();
            Session["UrltoFw"] = "ReportSaleOutM.aspx";

            if (_currentUserEmpNo == "")
                Response.Redirect("login.aspx");
            if (!IsPostBack)
            {
                LoadAU();
                DBAction.SetSelected(dropYear, DateTime.Now.Year.ToString());
                //BuildTable(dropYear.SelectedValue, "%", "%", "%");
            }
        }
        protected string GetUserLoginEmpNo()
        {
            string t = "";
            try
            {
                if (Session["UserLoginInfo"] != null)
                {
                    Entities db = new Entities();
                    var u = (TB_USER[])Session["UserLoginInfo"];
                    int _UID = Convert.ToInt32(u.Single().ID);
                    var au = from ft in db.TB_USER_LEVEL
                             where ft.USER_ID == _UID
                             select ft;
                    int _le = Convert.ToInt32(au.Single().LEVEL_ID);
                    var le = from xx in db.TB_LEVEL
                             where xx.ID == _le
                             select xx;
                    _currentUserAuthority = le.Single().REMARK;
                    _currentUserRegion = u.Single().REGION_ID;
                    return u.Single().EMPNO;
                }
            }
            catch (Exception)
            {
                t = "";
            }
            return t;
        }
        protected void LoadAU()
        {
            Entities db = new Entities();
            var au = (from t in db.TB_CM_MODEL_CAT
                      select new { au = t.PROD_L1 }).Distinct();
            ListItem i = new ListItem();
            i.Value = "0";
            i.Text = "Select";
            dropAu.Items.Add(i);
            au = au.OrderBy(k => k.au);
            foreach (var item in au)
            {
                i = new ListItem();
                i.Value = item.au;
                i.Text = item.au.ToUpper();
                dropAu.Items.Add(i);
            }
        }
        protected void BuildTable(string year, string _BillCode, string _product, string _model, string _curUserRegion, string region)
        {
            DataTable dt = new DataTable();
            dt = DBAction.runStore("PK_SOM_MST.PR_SO_MTH_INQ", "uiroi", "p_yyyy", year, "p_billto_code", _BillCode, "p_product", _product, "p_model", _model, "p_user_region", _curUserRegion,
                "p_region_id", region, null, null, null, null, null, null
                , null, null, null, null, null, null, null, null, null, null, null, null);//from.ToString("yyyyMMdd"), to.ToString("yyyyMMdd"), _BillCode, _product, _model));
            try
            {
                if (dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0]["Error"].ToString()))
                {
                    LCUV.MessageBox.Show(dt.Rows[0]["Error"].ToString());
                    return;
                }
            }
            catch (Exception)
            {
                //Column 'Error' does not belong to table <-- ko co' loi j va co' data , thi ko lam j
            }
            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
        }
        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "var btn=document.getElementById('btnFind');btn.click();", true);
            string year = dropYear.SelectedValue;
            string s = "%";
            string p = "%";
            string m = "%";
            string region = "%";
            if (dropRegion2.SelectedValue != "00")
                region = dropRegion2.SelectedValue;
            if (!string.IsNullOrEmpty(txtBillCode.Text.Trim()))
                s = txtBillCode.Text.Trim().ToUpper();
            if (!string.IsNullOrEmpty(txtModel.Text.Trim()))
                m = txtModel.Text.Trim().ToUpper();
            if (dropAu.SelectedValue != "0")
                p = dropAu.SelectedValue;

            DataTable dt = new DataTable();
            dt = DBAction.runStore("PK_SOM_MST.PR_SO_MTH_INQ", "uiroi", "p_yyyy", year, "p_billto_code",
                s, "p_product", p, "p_model", m, "p_user_region", _currentUserRegion,
                "p_region_id", region, null, null, null, null, null, null
                , null, null, null, null, null, null, null, null, null, null, null, null);
            try
            {
                if (dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0]["Error"].ToString()))
                {
                    LCUV.MessageBox.Show(dt.Rows[0]["Error"].ToString());
                    return;
                }
            }
            catch (Exception)
            {
                //Column 'Error' does not belong to table <-- ko co' loi j va co' data , thi ko lam j
            }
            RadGrid1.DataSource = dt;
        }
        protected void btnFind_Click(object sender, EventArgs e)
        {
            string year = dropYear.SelectedValue;
            string s = "%";
            string p = "%";
            string m = "%";
            string region = "%";
            if (dropRegion2.SelectedValue != "00")
                region = dropRegion2.SelectedValue;
            if (!string.IsNullOrEmpty(txtBillCode.Text.Trim()))
                s = txtBillCode.Text.Trim().ToUpper();
            if (!string.IsNullOrEmpty(txtModel.Text.Trim()))
                m = txtModel.Text.Trim().ToUpper();
            if (dropAu.SelectedValue != "0")
                p = dropAu.SelectedValue;

            BuildTable(year, s, p, m, _currentUserRegion, region);

        }
        protected void RadGrid1_GroupsChanging(object sender, GridGroupsChangingEventArgs e)
        {
            if (e.Action == GridGroupsChangingAction.Group)
                RadGrid1.MasterTableView.GetColumnSafe(e.Expression.GroupByFields[0].FieldName).Visible = false;
            else
                if (e.Action == GridGroupsChangingAction.Ungroup)
                    RadGrid1.MasterTableView.GetColumnSafe(e.Expression.GroupByFields[0].FieldName).Visible = true;
        }

        protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
        {
            if (e.Column is GridBoundColumn)
            {
                (e.Column as GridBoundColumn).DataFormatString = "<nobr>{0}</nobr>"; //auto width
               
            }
        }
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            RadGrid1.ExportSettings.IgnorePaging = true;
            RadGrid1.ExportSettings.OpenInNewWindow = true;
            RadGrid1.ExportSettings.FileName = "data";
            RadGrid1.MasterTableView.ExportToExcel();
        }
        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (_currentUserRegion != "00")
            {
                GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("REGION");
                if (column != null)
                {
                    column.Visible = false;
                }
            }
        }
    }
}